<?php

namespace home\controllers; // 必须加上命名空间

/**
 * 博客控制器(测试样例)
 *
 * @author vincent
 *        
 */
class BlogsController extends \Controller {
	/**
	 * 默认动作
	 */
	function indexAction() {
		// \TaskQueue::addBackGroundTask ( 'log_sth', 'test' );
		$blogModel = $this->loadModel ( 'BlogModel' );
		$page = ($this->input->get ( 'page' ) === NULL) ? 1 : $this->input->get ( 'page' );
		$pageSize = 2;
		$this->render ( 'blog/index', array (
				'curPage' => $page,
				'pageSize' => $pageSize,
				'maxPage' => $blogModel->getMaxPage ( $pageSize ),
				'paginationHref' => 'blogs/index',
				'blogList' => $blogModel->getPage ( $page, $pageSize ) 
		) );
	}
	/**
	 * 保存博客
	 */
	function saveAction() {
		$blogModel = $this->loadModel ( 'BlogModel' );
		$id = uniqid ();
		// 事务测试
		echo "事务测试开始<br>";
		try {
			\Model::beginTransaction ();
			// --------------特意插入两个主键相同的记录----------------
			$blogModel->insert ( array (
					'id' => 1,
					'title' => $this->input->get ( 'title' ),
					'content' => $this->input->get ( 'content' ),
					'create_time' => \Mikasa::getTime () 
			) );
			echo "insert 1<br>";
			$blogModel->insert ( array (
					'id' => 2,
					'title' => $this->input->get ( 'title' ),
					'content' => $this->input->get ( 'content' ),
					'create_time' => \Mikasa::getTime () 
			) );
			echo "insert 2<br>";
			$blogModel->insert ( array (
					'id' => 3,
					'title' => $this->input->get ( 'title' ),
					'content' => $this->input->get ( 'content' ),
					'create_time' => \Mikasa::getTime () 
			) );
			echo "insert 3<br>";
			$blogModel->insert ( array (
					'id' => $id,
					'title' => $this->input->get ( 'title' ),
					'content' => $this->input->get ( 'content' ),
					'create_time' => \Mikasa::getTime () 
			) );
			echo "insert 4<br>";
			$blogModel->insert ( array (
					'id' => $id,
					'title' => $this->input->get ( 'title' ),
					'content' => $this->input->get ( 'content' ),
					'create_time' => \Mikasa::getTime () 
			) );
			echo "insert 5<br>";
			\Model::commit ();
		}
		catch ( \Exception $e ) {
			\Model::rollBack ();
			echo "事务回滚成功<br>";
		}
		$blogModel->insert ( array (
				'id' => $id,
				'title' => $this->input->get ( 'title' ),
				'content' => $this->input->get ( 'content' ),
				'create_time' => \Mikasa::getTime () 
		) );
		
		$this->render ( 'blog/index', array (
				'blogList' => $blogModel->select () 
		) );
	}
	function deleteAction() {
		$this->loadModel ( 'BlogModel' )->delete ( array (
				array (
						'id',
						'=',
						$this->input->get ( 'id' ) 
				) 
		) );
		$this->redirect ( 'blogs' );
	}
	/**
	 * 编辑博客
	 */
	function editAction() {
		$data ['blog'] = $this->loadModel ( 'BlogModel' )->field ( '*' )->findByIndex ( $this->input->get ( 'id' ), 'id' );
		$this->render ( 'blog/edit', $data );
	}
	/**
	 * 更新博客
	 */
	function updateAction() {
		$data ['blog'] = $this->loadModel ( 'BlogModel' )->setField ( 'title', $this->input->get ( 'title' ) )->setField ( 'content', $this->input->get ( 'content' ) )->update ( array (
				array (
						'id',
						'=',
						$this->input->get ( 'id' ) 
				) 
		) );
		
		$this->loadModel ( 'blogModel' )->inc ( 'version' );
		$this->redirect ( 'blogs' );
	}
	function testAction() {
		require SYSTEM_PATH . '/TaskQueue.php';
		\TaskQueue::addBackGroundTask ( 'hello', $this->input->get ( 'param' ) );
		// $this->redirect ( 'blogs/index', '', 3 );
		// var_dump ( $this->loadModel ( 'BlogModel' )->batchInsert ( array (
		// array (
		// 'id' => uniqid (),
		// 'title' => '1',
		// 'content' => '1',
		// 'create_time' => time ()
		// ),
		// array (
		// 'id' => uniqid (),
		// 'title' => '2',
		// 'content' => '2',
		// 'create_time' => time ()
		// ),
		// array (
		// 'id' => uniqid (),
		// 'title' => '3',
		// 'content' => '3',
		// 'create_time' => time ()
		// ),
		// array (
		// 'id' => uniqid (),
		// 'title' => '4',
		// 'content' => '4',
		// 'create_time' => time ()
		// )
		// ) ) );
		
		// $blogModel = $this->loadModel ( 'BlogModel' );
		// $blogModel->id = uniqid ();
		// $blogModel->title = "'''''''''''''''''''''''";
		// $blogModel->content = "'";
		// $blogModel->create_time = \Mikasa::getTime ();
		// $blogModel->save ();
	}
	function deleteAllAction() {
		var_dump ( $this->loadModel ( 'BlogModel' )->delete () );
	}
	/**
	 * 查找博客
	 */
	function searchAction() {
		\TaskQueue::addBackGroundTask ( 'send_mail', array (
				'mail' => '710852740@qq.com',
				'subject' => 'test',
				'body' => $this->input->get ( 'content' ) 
		) );
		
		$blogModel = $this->loadModel ( 'BlogModel' );
		$page = ($this->input->get ( 'page' ) === NULL) ? 1 : $this->input->get ( 'page' );
		$pageSize = 4;
		$this->render ( 'blog/index', array (
				'curPage' => $page,
				'pageSize' => $pageSize,
				'maxPage' => $blogModel->getMaxPage ( $pageSize ),
				'paginationHref' => 'blogs/search',
				'params' => array (
						'content' => $this->input->get ( 'content' ) 
				),
				'keyWord' => $this->input->get ( 'content' ),
				'blogList' => $blogModel->field ( '*' )->whereLike ( 'content', '%' . $this->input->get ( 'content' ) . '%' )->whereLike ( 'title', '%' . $this->input->get ( 'content' ) . '%', 'OR' )->page ( $page, $pageSize )->select () 
		) );
	}
	function getByIdAction() {
		var_dump ( $this->loadModel ( 'BlogModel' )->whereEqual ( 'id', $this->input->get ( 'id' ) )->select () );
	}
}